package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.model.Problem;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.ProblemImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/crebas/problem")
@RequiredArgsConstructor
public class ProblemResource {
    private final ProblemImpl problemService;
    // 创建一个新的问题管理
    @PostMapping("/createProblem")
    public ResponseEntity<Response> createProblem(@RequestBody Problem problem) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newProblem", problemService.createProb(problem)))
                        .message("新建问题管理")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }
    // 删除一个问题管理
    @DeleteMapping("/deleteProblem/{id2}")
    public ResponseEntity<Response> deleteProblem(@PathVariable String id2) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("deleteResult",problemService.deleteProb(id2)))
                        .message("删除问题管理")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    // 更新一个问题管理
    @PostMapping("/updateProblem")
    public ResponseEntity<Response> updateProblem(@RequestBody Problem problem) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedProblem",problemService.updateProb(problem)))
                        .message("更新问题管理")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有问题管理
    @GetMapping("/listProblem/{limitpro}")
    public ResponseEntity<Response> listProblems(@PathVariable int limitpro) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("problems",problemService.listProb(limitpro)))
                        .message("获取"+limitpro+"个问题管理")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    // 列出所有问题管理
    @GetMapping("/listAll")
    public ResponseEntity<Response> listAll() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("problems",problemService.listAll()))
                        .message("获取所有问题管理")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有问题管理
    @GetMapping("/searchProbByProblemState/{projectState}")
    public ResponseEntity<Response> searchProbByProblemState(@PathVariable String projectState) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("problems",problemService.searchProbByProblemState(projectState)))
                        .message("获取所有问题管理")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
}
